/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { Modal, message, Typography } from 'antd';
import type { ActionType } from '@ant-design/pro-table';
import ProForm, { ModalForm, ProFormText, ProFormRadio, ProFormSelect } from '@ant-design/pro-form';
import { saveCarryPrice, getBrandList } from '@/services/jdk/register';
import { getPriceStorage } from '@/components/_pages/jdk/addLeague/_utils';
import styles from './index.less';

interface RefuseModalProps {
  rowKeys: number[];
  setPriceVisible: boolean;
  tableRef: ActionType | any;
  onCancelModal: () => void;
}

const { Paragraph } = Typography;
const RefuseModal: React.FC<RefuseModalProps> = ({
  rowKeys,
  setPriceVisible,
  tableRef,
  onCancelModal,
}) => {
  const [visible, setVisible] = useState<boolean>(false);
  const [transport, setTransport] = useState<boolean>(false);
  const formatList = (data: { nickname: string; phone: string }[]) => {
    return data.map((item) => item.nickname + ' ' + item.phone).join('、');
  };
  const onChange = (brand: string) => setTransport(brand === 'kb');
  const handleCancel = () => onCancelModal();
  useEffect(() => {
    setTransport(false);
    setVisible(setPriceVisible);
  }, [setPriceVisible]);
  useEffect(() => {
    return Modal.destroyAll();
  }, []);
  return (
    <ModalForm
      title='温馨提示'
      className={styles.main}
      width={480}
      layout='horizontal'
      visible={visible}
      modalProps={{
        onCancel: handleCancel,
        destroyOnClose: true,
        bodyStyle: {
          display: 'flex',
          justifyContent: 'center',
        },
      }}
      onFinish={async (values) => {
        const { brand, base_price, expire_type } = values;
        const config: {
          brand: string;
          expire_type: string;
          user_ids: number[];
          base_price?: number;
        } = {
          brand,
          expire_type,
          user_ids: rowKeys,
        };
        if (base_price) {
          config.base_price = getPriceStorage(base_price);
        }
        const { code, data, msg } = await saveCarryPrice({ config });
        if (code == 0) {
          onCancelModal();
          message.success('操作成功');
          tableRef.current?.reload();
        } else if (code == 55555) {
          const { success_num, delivery_brand, had_setting = [], unbind_delivery = [] } = data;
          Modal.info({
            title: '温馨提示',
            content: (
              <div>
                <Paragraph strong>{success_num} 个商家设置成功 ！</Paragraph>
                {had_setting.length > 0 && (
                  <>
                    <Paragraph strong>
                      以下{had_setting.length}个商家{delivery_brand}
                      运力已设置过基础价，本次不生效：
                    </Paragraph>
                    <Paragraph>{formatList(had_setting)}</Paragraph>
                  </>
                )}
                {unbind_delivery.length > 0 && (
                  <>
                    <Paragraph strong>
                      以下{unbind_delivery.length}个商家未绑定{delivery_brand}运力：
                    </Paragraph>
                    <Paragraph>{formatList(unbind_delivery)}</Paragraph>
                  </>
                )}
              </div>
            ),
            onOk() {},
            width: 600,
          });
          onCancelModal();
          tableRef.current?.reload();
        } else {
          message.error(msg);
        }
      }}
      initialValues={{
        brand: 'sftc',
        expire_type: '1m',
      }}
    >
      <ProForm.Group>
        <ProFormText
          name='base_price'
          placeholder='请输入金额'
          allowClear
          getValueFromEvent={(e) => {
            return e.target.value.replace(/\s+/gi, '');
          }}
          addonBefore={[
            <ProFormSelect
              key='brand'
              name='brand'
              style={{ margin: '0' }}
              fieldProps={{
                onChange,
                allowClear: false,
              }}
              request={getBrandList}
            />,
          ]}
          rules={[{ required: true, message: '请输入金额' }]}
          addonAfter={<div>元/单</div>}
        />
      </ProForm.Group>
      {!transport && (
        <ProFormRadio.Group
          name='expire_type'
          label='设置有效期'
          options={[
            {
              label: '1个月',
              value: '1m',
            },
            {
              label: '3个月',
              value: '3m',
            },
            {
              label: '6个月',
              value: '6m',
            },
          ]}
        />
      )}
    </ModalForm>
  );
};

export default RefuseModal;
